home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / mozilla-firefox / components / nsHelperAppDlg.js < prev    next >
Text File  |  2006-05-08  |  36KB  |  929 lines

  1. /*
  2. //@line 42 "/var/tmp/portage/mozilla-firefox-1.5.0.3/work/mozilla/toolkit/mozapps/downloads/src/nsHelperAppDlg.js.in"
  3. */
  4.  
  5. /* This file implements the nsIHelperAppLauncherDialog interface.
  6.  *
  7.  * The implementation consists of a JavaScript "class" named nsUnknownContentTypeDialog,
  8.  * comprised of:
  9.  *   - a JS constructor function
  10.  *   - a prototype providing all the interface methods and implementation stuff
  11.  *
  12.  * In addition, this file implements an nsIModule object that registers the
  13.  * nsUnknownContentTypeDialog component.
  14.  */
  15.  
  16.  
  17. /* ctor
  18.  */
  19. function nsUnknownContentTypeDialog() {
  20.     // Initialize data properties.
  21.     this.mLauncher = null;
  22.     this.mContext  = null;
  23.     this.mSourcePath = null;
  24.     this.chosenApp = null;
  25.     this.givenDefaultApp = false;
  26.     this.updateSelf = true;
  27.     this.mTitle    = "";
  28. }
  29.  
  30. nsUnknownContentTypeDialog.prototype = {
  31.     nsIMIMEInfo  : Components.interfaces.nsIMIMEInfo,
  32.  
  33.     // This "class" supports nsIHelperAppLauncherDialog, and nsISupports.
  34.     QueryInterface: function (iid) {
  35.         if (!iid.equals(Components.interfaces.nsIHelperAppLauncherDialog) &&
  36.             !iid.equals(Components.interfaces.nsISupports)) {
  37.             throw Components.results.NS_ERROR_NO_INTERFACE;
  38.         }
  39.         return this;
  40.     },
  41.  
  42.     // ---------- nsIHelperAppLauncherDialog methods ----------
  43.  
  44.     // show: Open XUL dialog using window watcher.  Since the dialog is not
  45.     //       modal, it needs to be a top level window and the way to open
  46.     //       one of those is via that route).
  47.     show: function(aLauncher, aContext, aReason)  {
  48.       this.mLauncher = aLauncher;
  49.       this.mContext  = aContext;
  50.       // Display the dialog using the Window Watcher interface.
  51.       
  52.       var ir = aContext.QueryInterface(Components.interfaces.nsIInterfaceRequestor);
  53.       var dwi = ir.getInterface(Components.interfaces.nsIDOMWindowInternal);
  54.       var ww = Components.classes["@mozilla.org/embedcomp/window-watcher;1"]
  55.                 .getService(Components.interfaces.nsIWindowWatcher);
  56.       this.mDialog = ww.openWindow(dwi,
  57.                                    "chrome://mozapps/content/downloads/unknownContentType.xul",
  58.                                    null,
  59.                                    "chrome,centerscreen,titlebar,dialog=yes,dependent",
  60.                                    null);
  61.       // Hook this object to the dialog.
  62.       this.mDialog.dialog = this;
  63.       
  64.       // Hook up utility functions. 
  65.       this.getSpecialFolderKey = this.mDialog.getSpecialFolderKey;
  66.       
  67.       // Watch for error notifications.
  68.       this.progressListener.helperAppDlg = this;
  69.       this.mLauncher.setWebProgressListener(this.progressListener);
  70.     },
  71.  
  72.     // promptForSaveToFile:  Display file picker dialog and return selected file.
  73.     //                       This is called by the External Helper App Service
  74.     //                       after the ucth dialog calls |saveToDisk| with a null
  75.     //                       target filename (no target, therefore user must pick).
  76.     //
  77.     //                       Alternatively, if the user has selected to have all
  78.     //                       files download to a specific location, return that
  79.     //                       location and don't ask via the dialog. 
  80.     //
  81.     // Note - this function is called without a dialog, so it cannot access any part
  82.     // of the dialog XUL as other functions on this object do. 
  83.     promptForSaveToFile: function(aLauncher, aContext, aDefaultFile, aSuggestedFileExtension) {
  84.       var result = "";
  85.       
  86.       this.mLauncher = aLauncher;
  87.  
  88.       // If the user is always downloading to the same location, the default download
  89.       // folder is stored in preferences. If a value is found stored, use that 
  90.       // automatically and don't ask via a dialog. 
  91.       var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
  92.       var autodownload = prefs.getBoolPref("browser.download.useDownloadDir");
  93.       if (autodownload) {
  94.         function getSpecialFolderKey(aFolderType) 
  95.         {
  96.           if (aFolderType == "Desktop")
  97.             return "Desk";
  98.         
  99.           if (aFolderType != "Downloads")
  100.             throw "ASSERTION FAILED: folder type should be 'Desktop' or 'Downloads'";
  101.         
  102. //@line 147 "/var/tmp/portage/mozilla-firefox-1.5.0.3/work/mozilla/toolkit/mozapps/downloads/src/nsHelperAppDlg.js.in"
  103.           return "Home";
  104. //@line 150 "/var/tmp/portage/mozilla-firefox-1.5.0.3/work/mozilla/toolkit/mozapps/downloads/src/nsHelperAppDlg.js.in"
  105.         }
  106.         
  107.         function getDownloadsFolder(aFolder)
  108.         {
  109.           var fileLocator = Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties);
  110.  
  111.           var dir = fileLocator.get(getSpecialFolderKey(aFolder), Components.interfaces.nsILocalFile);
  112.           
  113.           var bundle = Components.classes["@mozilla.org/intl/stringbundle;1"].getService(Components.interfaces.nsIStringBundleService);
  114.           bundle = bundle.createBundle("chrome://mozapps/locale/downloads/unknownContentType.properties");
  115.  
  116.           var description = bundle.GetStringFromName("myDownloads");
  117.           if (aFolder != "Desktop")
  118.             dir.append(description);
  119.             
  120.           return dir;
  121.         }
  122.  
  123.         var defaultFolder = null;
  124.         switch (prefs.getIntPref("browser.download.folderList")) {
  125.         case 0:
  126.           defaultFolder = getDownloadsFolder("Desktop");
  127.           break;
  128.         case 1:
  129.           defaultFolder = getDownloadsFolder("Downloads");
  130.           break;
  131.         case 2:
  132.           defaultFolder = prefs.getComplexValue("browser.download.dir", Components.interfaces.nsILocalFile);
  133.           break;
  134.         }
  135.         
  136.         result = this.validateLeafName(defaultFolder, aDefaultFile, aSuggestedFileExtension);
  137.       }
  138.       
  139.       if (!result) {
  140.         // Use file picker to show dialog.
  141.         var nsIFilePicker = Components.interfaces.nsIFilePicker;
  142.         var picker = Components.classes["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
  143.  
  144.         var bundle = Components.classes["@mozilla.org/intl/stringbundle;1"].getService(Components.interfaces.nsIStringBundleService);
  145.         bundle = bundle.createBundle("chrome://mozapps/locale/downloads/unknownContentType.properties");
  146.  
  147.         var windowTitle = bundle.GetStringFromName("saveDialogTitle");
  148.         var parent = aContext.QueryInterface(Components.interfaces.nsIInterfaceRequestor).getInterface(Components.interfaces.nsIDOMWindowInternal);
  149.         picker.init(parent, windowTitle, nsIFilePicker.modeSave);
  150.         picker.defaultString = aDefaultFile;
  151.  
  152.         if (aSuggestedFileExtension) {
  153.           // aSuggestedFileExtension includes the period, so strip it
  154.           picker.defaultExtension = aSuggestedFileExtension.substring(1);
  155.         } 
  156.         else {
  157.           try {
  158.             picker.defaultExtension = this.mLauncher.MIMEInfo.primaryExtension;
  159.           } 
  160.           catch (ex) { }
  161.         }
  162.  
  163.         var wildCardExtension = "*";
  164.         if (aSuggestedFileExtension) {
  165.           wildCardExtension += aSuggestedFileExtension;
  166.           picker.appendFilter(this.mLauncher.MIMEInfo.description, wildCardExtension);
  167.         }
  168.  
  169.         picker.appendFilters( nsIFilePicker.filterAll );
  170.  
  171.         // Pull in the user's preferences and get the default download directory.
  172.         var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
  173.         try {
  174.           var startDir = prefs.getComplexValue("browser.download.dir", Components.interfaces.nsILocalFile);
  175.           if (startDir.exists()) {
  176.             picker.displayDirectory = startDir;
  177.           }
  178.         } 
  179.         catch(exception) { }
  180.  
  181.         var dlgResult = picker.show();
  182.  
  183.         if (dlgResult == nsIFilePicker.returnCancel) {
  184.           // null result means user cancelled.
  185.           return null;
  186.         }
  187.  
  188.  
  189.         // Be sure to save the directory the user chose through the Save As... 
  190.         // dialog  as the new browser.download.dir
  191.         result = picker.file;
  192.  
  193.         if (result) {
  194.           var newDir = result.parent;
  195.           prefs.setComplexValue("browser.download.dir", Components.interfaces.nsILocalFile, newDir);
  196.         }
  197.       }
  198.       return result;
  199.     },
  200.     
  201.     validateLeafName: function (aLocalFile, aLeafName, aFileExt)
  202.     {
  203.       if (!aLocalFile || !aLocalFile.exists())
  204.         return null;
  205.  
  206.       if (aLeafName == "")
  207.         aLeafName = "unnamed" + (aFileExt ? "." + aFileExt : "");
  208.       aLocalFile.append(aLeafName);
  209.  
  210.       this.makeFileUnique(aLocalFile);
  211.  
  212.       if (aLocalFile.isExecutable() && !this.mLauncher.targetFile.isExecutable()) {
  213.         var f = aLocalFile.clone();
  214.         aLocalFile.leafName = aLocalFile.leafName + "." + this.mLauncher.MIMEInfo.primaryExtension; 
  215.  
  216.         f.remove(false);
  217.         this.makeFileUnique(aLocalFile);
  218.       }
  219.       return aLocalFile;
  220.     },
  221.  
  222.     makeFileUnique: function (aLocalFile)
  223.     {
  224.       try {
  225.         // Since we're automatically downloading, we don't get the file picker's 
  226.         // logic to check for existing files, so we need to do that here.
  227.         //
  228.         // Note - this code is identical to that in 
  229.         //   toolkit/content/contentAreaUtils.js.
  230.         // If you are updating this code, update that code too! We can't share code
  231.         // here since this is called in a js component. 
  232.         var collisionCount = 0;
  233.         while (aLocalFile.exists()) {
  234.           collisionCount++;
  235.           if (collisionCount == 1) {
  236.             // Append "(2)" before the last dot in (or at the end of) the filename
  237.             // special case .ext.gz etc files so we don't wind up with .tar(2).gz
  238.             if (aLocalFile.leafName.match(/\.[^\.]{1,3}\.(gz|bz2|Z)$/i)) {
  239.               aLocalFile.leafName = aLocalFile.leafName.replace(/\.[^\.]{1,3}\.(gz|bz2|Z)$/i, "(2)$&");
  240.             }
  241.             else {
  242.               aLocalFile.leafName = aLocalFile.leafName.replace(/(\.[^\.]*)?$/, "(2)$&");
  243.             }
  244.           }
  245.           else {
  246.             // replace the last (n) in the filename with (n+1)
  247.             aLocalFile.leafName = aLocalFile.leafName.replace(/^(.*\()\d+\)/, "$1" + (collisionCount+1) + ")");
  248.           }
  249.         }
  250.         aLocalFile.create(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 0600);
  251.       }
  252.       catch (e) {
  253.         dump("*** exception in validateLeafName: " + e + "\n");
  254.         if (aLocalFile.leafName == "" || aLocalFile.isDirectory()) {
  255.           aLocalFile.append("unnamed");
  256.           if (aLocalFile.exists())
  257.             aLocalFile.createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 0600);
  258.         }
  259.       }
  260.     },
  261.     
  262.     // ---------- implementation methods ----------
  263.  
  264.     // Web progress listener so we can detect errors while mLauncher is
  265.     // streaming the data to a temporary file.
  266.     progressListener: {
  267.         // Implementation properties.
  268.         helperAppDlg: null,
  269.  
  270.         // nsIWebProgressListener methods.
  271.         // Look for error notifications and display alert to user.
  272.         onStatusChange: function( aWebProgress, aRequest, aStatus, aMessage ) {
  273.             if ( aStatus != Components.results.NS_OK ) {
  274.                 // Get prompt service.
  275.                 var prompter = Components.classes[ "@mozilla.org/embedcomp/prompt-service;1" ]
  276.                                    .getService( Components.interfaces.nsIPromptService );
  277.                 // Display error alert (using text supplied by back-end).
  278.                 prompter.alert( this.dialog, this.helperAppDlg.mTitle, aMessage );
  279.  
  280.                 // Close the dialog.
  281.                 this.helperAppDlg.onCancel();
  282.                 if ( this.helperAppDlg.mDialog ) {
  283.                     this.helperAppDlg.mDialog.close();
  284.                 }
  285.             }
  286.         },
  287.  
  288.         // Ignore onProgressChange, onStateChange, onLocationChange, and onSecurityChange notifications.
  289.         onProgressChange: function( aWebProgress,
  290.                                     aRequest,
  291.                                     aCurSelfProgress,
  292.                                     aMaxSelfProgress,
  293.                                     aCurTotalProgress,
  294.                                     aMaxTotalProgress ) {
  295.         },
  296.  
  297.         onProgressChange64: function( aWebProgress,
  298.                                       aRequest,
  299.                                       aCurSelfProgress,
  300.                                       aMaxSelfProgress,
  301.                                       aCurTotalProgress,
  302.                                       aMaxTotalProgress ) {
  303.         },
  304.  
  305.  
  306.  
  307.         onStateChange: function( aWebProgress, aRequest, aStateFlags, aStatus ) {
  308.         },
  309.  
  310.         onLocationChange: function( aWebProgress, aRequest, aLocation ) {
  311.         },
  312.  
  313.         onSecurityChange: function( aWebProgress, aRequest, state ) {
  314.         }
  315.     },
  316.  
  317.     // initDialog:  Fill various dialog fields with initial content.
  318.     initDialog : function() {
  319.       // Put file name in window title.
  320.       var suggestedFileName = this.mLauncher.suggestedFileName;
  321.  
  322.       // Some URIs do not implement nsIURL, so we can't just QI.
  323.       var url   = this.mLauncher.source;
  324.       var fname = "";
  325.       this.mSourcePath = url.prePath;
  326.       try {
  327.           url = url.QueryInterface( Components.interfaces.nsIURL );
  328.           // A url, use file name from it.
  329.           fname = url.fileName;
  330.           this.mSourcePath += url.directory;
  331.       } catch (ex) {
  332.           // A generic uri, use path.
  333.           fname = url.path;
  334.           this.mSourcePath += url.path;
  335.       }
  336.  
  337.       if (suggestedFileName)
  338.         fname = suggestedFileName;
  339.       
  340.       var displayName = fname.replace(/ +/g, " ");
  341.  
  342.       this.mTitle = this.dialogElement("strings").getFormattedString("title", [displayName]);
  343.       this.mDialog.document.title = this.mTitle;
  344.  
  345.       // Put content type, filename and location into intro.
  346.       this.initIntro(url, fname, displayName);
  347.  
  348.       var iconString = "moz-icon://" + fname + "?size=16&contentType=" + this.mLauncher.MIMEInfo.MIMEType;
  349.       this.dialogElement("contentTypeImage").setAttribute("src", iconString);
  350.  
  351.       this.initAppAndSaveToDiskValues();
  352.  
  353.       // Initialize "always ask me" box. This should always be disabled
  354.       // and set to true for the ambiguous type application/octet-stream.
  355.       // We don't also check for application/x-msdownload here since we
  356.       // want users to be able to autodownload .exe files. 
  357.       var rememberChoice = this.dialogElement("rememberChoice");
  358.  
  359. //@line 424 "/var/tmp/portage/mozilla-firefox-1.5.0.3/work/mozilla/toolkit/mozapps/downloads/src/nsHelperAppDlg.js.in"
  360.       var mimeType = this.mLauncher.MIMEInfo.MIMEType;
  361.       if (mimeType == "application/octet-stream" || 
  362.           mimeType == "application/x-msdownload" ||
  363.           this.mLauncher.targetFile.isExecutable()) {
  364.         rememberChoice.checked = false;
  365.         rememberChoice.disabled = true;
  366.       }
  367.       else {
  368.         rememberChoice.checked = !this.mLauncher.MIMEInfo.alwaysAskBeforeHandling;
  369.       }
  370.       this.toggleRememberChoice(rememberChoice);
  371.  
  372.       // XXXben - menulist won't init properly, hack. 
  373.       var openHandler = this.dialogElement("openHandler");
  374.       openHandler.parentNode.removeChild(openHandler);
  375.       var openHandlerBox = this.dialogElement("openHandlerBox");
  376.       openHandlerBox.appendChild(openHandler);
  377.  
  378.       this.mDialog.setTimeout("dialog.postShowCallback()", 0);
  379.       
  380.       this.mDialog.document.documentElement.getButton("accept").disabled = true;
  381.       const nsITimer = Components.interfaces.nsITimer;
  382.       this._timer = Components.classes["@mozilla.org/timer;1"]
  383.                               .createInstance(nsITimer);
  384.       this._timer.initWithCallback(this, 250, nsITimer.TYPE_ONE_SHOT);
  385.     },
  386.     
  387.     _timer: null,
  388.     notify: function (aTimer) {
  389.       if (!this._blurred)
  390.         this.mDialog.document.documentElement.getButton('accept').disabled = false;
  391.       this._delayExpired = true;
  392.     },
  393.     
  394.     postShowCallback: function () {
  395.       this.mDialog.sizeToContent();
  396.  
  397.       // Set initial focus
  398.       this.dialogElement("mode").focus();
  399.     },
  400.  
  401.     // initIntro:
  402.     initIntro: function(url, filename, displayname) {
  403.         this.dialogElement( "location" ).value = displayname;
  404.         this.dialogElement( "location" ).setAttribute("realname", filename);
  405.         this.dialogElement( "location" ).setAttribute("tooltiptext", displayname);
  406.  
  407.         // if mSourcePath is a local file, then let's use the pretty path name instead of an ugly
  408.         // url...
  409.         var pathString = this.mSourcePath;
  410.         try 
  411.         {
  412.           var fileURL = url.QueryInterface(Components.interfaces.nsIFileURL);
  413.           if (fileURL)
  414.           {
  415.             var fileObject = fileURL.file;
  416.             if (fileObject)
  417.             {
  418.               var parentObject = fileObject.parent;
  419.               if (parentObject)
  420.               {
  421.                 pathString = parentObject.path;
  422.               }
  423.             }
  424.           }
  425.         } catch(ex) {}
  426.  
  427.         if (pathString == this.mSourcePath)
  428.         {
  429.           // wasn't a fileURL
  430.           var tmpurl = url.clone(); // don't want to change the real url
  431.           try {
  432.             tmpurl.userPass = "";
  433.           } catch (ex) {}
  434.           pathString = tmpurl.prePath;
  435.         }
  436.  
  437.         // Set the location text, which is separate from the intro text so it can be cropped
  438.         var location = this.dialogElement( "source" );
  439.         location.value = pathString;
  440.         location.setAttribute("tooltiptext", this.mSourcePath);
  441.         
  442.         // Show the type of file. 
  443.         var type = this.dialogElement("type");
  444.         var mimeInfo = this.mLauncher.MIMEInfo;
  445.         
  446.         // 1. Try to use the pretty description of the type, if one is available.
  447.         var typeString = mimeInfo.description;
  448.         
  449.         if (typeString == "") {
  450.           // 2. If there is none, use the extension to identify the file, e.g. "ZIP file"
  451.           var primaryExtension = "";
  452.           try {
  453.             primaryExtension = mimeInfo.primaryExtension;
  454.           }
  455.           catch (ex) {
  456.           }
  457.           if (primaryExtension != "")
  458.             typeString = primaryExtension.toUpperCase() + " file";
  459.           // 3. If we can't even do that, just give up and show the MIME type. 
  460.           else
  461.             typeString = mimeInfo.MIMEType;
  462.         }
  463.         
  464.         type.value = typeString;
  465.     },
  466.     
  467.     _blurred: false,
  468.     _delayExpired: false, 
  469.     onBlur: function(aEvent) {
  470.       if (aEvent.target != this.mDialog.document)
  471.         return;
  472.       this._blurred = true;
  473.       this.mDialog.document.documentElement.getButton("accept").disabled = true;
  474.     },
  475.     
  476.     onFocus: function(aEvent) {
  477.       if (aEvent.target != this.mDialog.document)
  478.         return;
  479.       this._blurred = false;
  480.       if (this._delayExpired) {
  481.         var script = "document.documentElement.getButton('accept').disabled = false";
  482.         this.mDialog.setTimeout(script, 250);
  483.       }
  484.     },
  485.  
  486.     // Returns true if opening the default application makes sense.
  487.     openWithDefaultOK: function() {
  488.         var result;
  489.  
  490.         // The checking is different on Windows...
  491. //@line 569 "/var/tmp/portage/mozilla-firefox-1.5.0.3/work/mozilla/toolkit/mozapps/downloads/src/nsHelperAppDlg.js.in"
  492.             // On other platforms, default is Ok if there is a default app.
  493.             // Note that nsIMIMEInfo providers need to ensure that this holds true
  494.             // on each platform.
  495.         return this.mLauncher.MIMEInfo.hasDefaultHandler;
  496. //@line 574 "/var/tmp/portage/mozilla-firefox-1.5.0.3/work/mozilla/toolkit/mozapps/downloads/src/nsHelperAppDlg.js.in"
  497.     },
  498.     
  499.     // Set "default" application description field.
  500.     initDefaultApp: function() {
  501.       // Use description, if we can get one.
  502.       var desc = this.mLauncher.MIMEInfo.defaultDescription;
  503.       if (desc) {
  504.         var defaultApp = this.dialogElement("strings").getFormattedString("defaultApp", [desc]);
  505.         this.dialogElement("defaultHandler").label = defaultApp;
  506.       }
  507.       else {
  508.         this.dialogElement("modeDeck").setAttribute("selectedIndex", "1");
  509.         // Hide the default handler item too, in case the user picks a 
  510.         // custom handler at a later date which triggers the menulist to show.
  511.         this.dialogElement("defaultHandler").hidden = true;
  512.       }
  513.     },
  514.  
  515.     // getPath:
  516.     getPath: function (aFile) {
  517. //@line 597 "/var/tmp/portage/mozilla-firefox-1.5.0.3/work/mozilla/toolkit/mozapps/downloads/src/nsHelperAppDlg.js.in"
  518.       return aFile.path;
  519. //@line 599 "/var/tmp/portage/mozilla-firefox-1.5.0.3/work/mozilla/toolkit/mozapps/downloads/src/nsHelperAppDlg.js.in"
  520.     },
  521.  
  522.     // initAppAndSaveToDiskValues:
  523.     initAppAndSaveToDiskValues: function() {
  524.       var modeGroup = this.dialogElement("mode");
  525.  
  526.       // We don't let users open .exe files or random binary data directly 
  527.       // from the browser at the moment because of security concerns. 
  528.       var openWithDefaultOK = this.openWithDefaultOK();
  529.       var mimeType = this.mLauncher.MIMEInfo.MIMEType;
  530.       if (this.mLauncher.targetFile.isExecutable() || (
  531.           (mimeType == "application/octet-stream" ||
  532.            mimeType == "application/x-msdownload") && 
  533.            !openWithDefaultOK)) {
  534.         this.dialogElement("open").disabled = true;
  535.         var openHandler = this.dialogElement("openHandler");
  536.         openHandler.disabled = true;
  537.         openHandler.selectedItem = null;
  538.         modeGroup.selectedItem = this.dialogElement("save");
  539.         return;
  540.       }
  541.     
  542.       // Fill in helper app info, if there is any.
  543.       this.chosenApp = this.mLauncher.MIMEInfo.preferredApplicationHandler;
  544.       // Initialize "default application" field.
  545.       this.initDefaultApp();
  546.  
  547.       var otherHandler = this.dialogElement("otherHandler");
  548.               
  549.       // Fill application name textbox.
  550.       if (this.chosenApp && this.chosenApp.path) {
  551.         otherHandler.setAttribute("path", this.getPath(this.chosenApp));
  552.         otherHandler.label = this.chosenApp.leafName;
  553.         otherHandler.hidden = false;
  554.       }
  555.  
  556.       var useDefault = this.dialogElement("useSystemDefault");
  557.       var openHandler = this.dialogElement("openHandler");
  558.       openHandler.selectedIndex = 0;
  559.  
  560.       if (this.mLauncher.MIMEInfo.preferredAction == this.nsIMIMEInfo.useSystemDefault) {
  561.         // Open (using system default).
  562.         modeGroup.selectedItem = this.dialogElement("open");
  563.       } else if (this.mLauncher.MIMEInfo.preferredAction == this.nsIMIMEInfo.useHelperApp) {
  564.         // Open with given helper app.
  565.         modeGroup.selectedItem = this.dialogElement("open");
  566.         openHandler.selectedIndex = 1;
  567.       } else {
  568.         // Save to disk.
  569.         modeGroup.selectedItem = this.dialogElement("save");
  570.       }
  571.       
  572.       // If we don't have a "default app" then disable that choice.
  573.       if (!openWithDefaultOK) {
  574.         var useDefault = this.dialogElement("defaultHandler");
  575.         var isSelected = useDefault.selected;
  576.         
  577.         // Disable that choice.
  578.         useDefault.hidden = true;
  579.         // If that's the default, then switch to "save to disk."
  580.         if (isSelected) {
  581.           openHandler.selectedIndex = 1;
  582.           modeGroup.selectedItem = this.dialogElement("save");
  583.         }
  584.       }
  585.       
  586.       // otherHandler is always disabled on Mac
  587. //@line 670 "/var/tmp/portage/mozilla-firefox-1.5.0.3/work/mozilla/toolkit/mozapps/downloads/src/nsHelperAppDlg.js.in"
  588.       otherHandler.nextSibling.hidden = otherHandler.nextSibling.nextSibling.hidden = false;
  589. //@line 672 "/var/tmp/portage/mozilla-firefox-1.5.0.3/work/mozilla/toolkit/mozapps/downloads/src/nsHelperAppDlg.js.in"
  590.       this.updateOKButton();
  591.     },
  592.  
  593.     // Returns the user-selected application
  594.     helperAppChoice: function() {
  595.       return this.chosenApp;
  596.     },
  597.     
  598.     get saveToDisk() {
  599.       return this.dialogElement("save").selected;
  600.     },
  601.     
  602.     get useOtherHandler() {
  603.       return this.dialogElement("open").selected && this.dialogElement("openHandler").selectedIndex == 1;
  604.     },
  605.     
  606.     get useSystemDefault() {
  607.       return this.dialogElement("open").selected && this.dialogElement("openHandler").selectedIndex == 0;
  608.     },
  609.     
  610.     toggleRememberChoice: function (aCheckbox) {
  611.         this.dialogElement("settingsChange").hidden = !aCheckbox.checked;
  612.         this.mDialog.sizeToContent();
  613.     },
  614.     
  615.     openHandlerCommand: function () {
  616.       var openHandler = this.dialogElement("openHandler");
  617.       if (openHandler.selectedItem.id == "choose")
  618.         this.chooseApp();
  619.       else
  620.         openHandler.setAttribute("lastSelectedItemID", openHandler.selectedItem.id);
  621.     },
  622.  
  623.     updateOKButton: function() {
  624.       var ok = false;
  625.       if (this.dialogElement("save").selected) {
  626.         // This is always OK.
  627.         ok = true;
  628.       } 
  629.       else if (this.dialogElement("open").selected) {
  630.         switch (this.dialogElement("openHandler").selectedIndex) {
  631.         case 0:
  632.           // No app need be specified in this case.
  633.           ok = true;
  634.           break;
  635.         case 1:
  636.           // only enable the OK button if we have a default app to use or if 
  637.           // the user chose an app....
  638.           ok = this.chosenApp || /\S/.test(this.dialogElement("otherHandler").getAttribute("path")); 
  639.         break;
  640.         }
  641.       }
  642.  
  643.       // Enable Ok button if ok to press.
  644.       this.mDialog.document.documentElement.getButton("accept").disabled = !ok;
  645.     },
  646.     
  647.     // Returns true iff the user-specified helper app has been modified.
  648.     appChanged: function() {
  649.       return this.helperAppChoice() != this.mLauncher.MIMEInfo.preferredApplicationHandler;
  650.     },
  651.  
  652.     updateMIMEInfo: function() {
  653.       var needUpdate = false;
  654.       // If current selection differs from what's in the mime info object,
  655.       // then we need to update.
  656.       if (this.saveToDisk) {
  657.         needUpdate = this.mLauncher.MIMEInfo.preferredAction != this.nsIMIMEInfo.saveToDisk;
  658.         if (needUpdate)
  659.           this.mLauncher.MIMEInfo.preferredAction = this.nsIMIMEInfo.saveToDisk;
  660.       } 
  661.       else if (this.useSystemDefault) {
  662.         needUpdate = this.mLauncher.MIMEInfo.preferredAction != this.nsIMIMEInfo.useSystemDefault;
  663.         if (needUpdate)
  664.           this.mLauncher.MIMEInfo.preferredAction = this.nsIMIMEInfo.useSystemDefault;
  665.       } 
  666.       else {
  667.         // For "open with", we need to check both preferred action and whether the user chose
  668.         // a new app.
  669.         needUpdate = this.mLauncher.MIMEInfo.preferredAction != this.nsIMIMEInfo.useHelperApp || this.appChanged();
  670.         if (needUpdate) {
  671.           this.mLauncher.MIMEInfo.preferredAction = this.nsIMIMEInfo.useHelperApp;
  672.           // App may have changed - Update application and description
  673.           var app = this.helperAppChoice();
  674.           this.mLauncher.MIMEInfo.preferredApplicationHandler = app;
  675.           this.mLauncher.MIMEInfo.applicationDescription = "";
  676.         }
  677.       }
  678.       // We will also need to update if the "always ask" flag has changed.
  679.       needUpdate = needUpdate || this.mLauncher.MIMEInfo.alwaysAskBeforeHandling != (!this.dialogElement("rememberChoice").checked);
  680.  
  681.       // One last special case: If the input "always ask" flag was false, then we always
  682.       // update.  In that case we are displaying the helper app dialog for the first
  683.       // time for this mime type and we need to store the user's action in the mimeTypes.rdf
  684.       // data source (whether that action has changed or not; if it didn't change, then we need
  685.       // to store the "always ask" flag so the helper app dialog will or won't display
  686.       // next time, per the user's selection).
  687.       needUpdate = needUpdate || !this.mLauncher.MIMEInfo.alwaysAskBeforeHandling;
  688.  
  689.       // Make sure mime info has updated setting for the "always ask" flag.
  690.       this.mLauncher.MIMEInfo.alwaysAskBeforeHandling = !this.dialogElement("rememberChoice").checked;
  691.  
  692.       return needUpdate;        
  693.     },
  694.     
  695.     // See if the user changed things, and if so, update the
  696.     // mimeTypes.rdf entry for this mime type.
  697.     updateHelperAppPref: function() {
  698.       var ha = new this.mDialog.HelperApps();
  699.       ha.updateTypeInfo(this.mLauncher.MIMEInfo);
  700.     },
  701.     
  702.     // onOK:
  703.     onOK: function() {
  704.       // Verify typed app path, if necessary.
  705.       if (this.useOtherHandler) {
  706.         var helperApp = this.helperAppChoice();
  707.         if (!helperApp || !helperApp.exists()) {
  708.           // Show alert and try again.        
  709.           var bundle = this.dialogElement("strings");                    
  710.           var msg = bundle.getFormattedString("badApp", [this.dialogElement("otherHandler").path]);
  711.           var svc = Components.classes["@mozilla.org/embedcomp/prompt-service;1"].getService(Components.interfaces.nsIPromptService);
  712.           svc.alert(this.mDialog, bundle.getString("badApp.title"), msg);
  713.  
  714.           // Disable the OK button.
  715.           this.mDialog.document.documentElement.getButton("accept").disabled = true;
  716.           this.dialogElement("mode").focus();          
  717.  
  718.           // Clear chosen application.
  719.           this.chosenApp = null;
  720.  
  721.           // Leave dialog up.
  722.           return false;
  723.         }
  724.       }
  725.         
  726.       // Remove our web progress listener (a progress dialog will be
  727.       // taking over).
  728.       this.mLauncher.setWebProgressListener(null);
  729.       
  730.       // saveToDisk and launchWithApplication can return errors in 
  731.       // certain circumstances (e.g. The user clicks cancel in the
  732.       // "Save to Disk" dialog. In those cases, we don't want to
  733.       // update the helper application preferences in the RDF file.
  734.       try {
  735.         var needUpdate = this.updateMIMEInfo();
  736.         
  737.         if (this.dialogElement("save").selected) {
  738.           // If we're using a default download location, create a path
  739.           // for the file to be saved to to pass to |saveToDisk| - otherwise
  740.           // we must ask the user to pick a save name.
  741.  
  742. //@line 838 "/var/tmp/portage/mozilla-firefox-1.5.0.3/work/mozilla/toolkit/mozapps/downloads/src/nsHelperAppDlg.js.in"
  743.           this.mLauncher.saveToDisk(null, false);
  744.         }
  745.         else
  746.           this.mLauncher.launchWithApplication(null, false);
  747.  
  748.         // Update user pref for this mime type (if necessary). We do not
  749.         // store anything in the mime type preferences for the ambiguous
  750.         // type application/octet-stream. We do NOT do this for 
  751.         // application/x-msdownload since we want users to be able to 
  752.         // autodownload these to disk. 
  753.         if (needUpdate && this.mLauncher.MIMEInfo.MIMEType != "application/octet-stream")
  754.           this.updateHelperAppPref();
  755.       } catch(e) { }
  756.  
  757.       // Unhook dialog from this object.
  758.       this.mDialog.dialog = null;
  759.  
  760.       // Close up dialog by returning true.
  761.       return true;
  762.     },
  763.  
  764.     // onCancel:
  765.     onCancel: function() {
  766.       // Remove our web progress listener.
  767.       this.mLauncher.setWebProgressListener(null);
  768.  
  769.       // Cancel app launcher.
  770.       try {
  771.         const NS_BINDING_ABORTED = 0x804b0002;
  772.         this.mLauncher.cancel(NS_BINDING_ABORTED);
  773.       } catch(exception) {
  774.       }
  775.  
  776.       // Unhook dialog from this object.
  777.       this.mDialog.dialog = null;
  778.  
  779.       // Close up dialog by returning true.
  780.       return true;
  781.     },
  782.  
  783.     // dialogElement:  Convenience. 
  784.     dialogElement: function(id) {
  785.       return this.mDialog.document.getElementById(id);
  786.     },
  787.  
  788.     // chooseApp:  Open file picker and prompt user for application.
  789.     chooseApp: function() {
  790.       var nsIFilePicker = Components.interfaces.nsIFilePicker;
  791.       var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
  792.       fp.init(this.mDialog,
  793.               this.dialogElement("strings").getString("chooseAppFilePickerTitle"),
  794.               nsIFilePicker.modeOpen);
  795.  
  796.       fp.appendFilters(nsIFilePicker.filterApps);
  797.  
  798.       if (fp.show() == nsIFilePicker.returnOK && fp.file) {
  799.         // Show the "handler" menulist since we have a (user-specified) 
  800.         // application now.
  801.         this.dialogElement("modeDeck").setAttribute("selectedIndex", "0");
  802.         
  803.         // Remember the file they chose to run.
  804.         this.chosenApp = fp.file;
  805.         // Update dialog.
  806.         var otherHandler = this.dialogElement("otherHandler");
  807.         otherHandler.removeAttribute("hidden");
  808.         otherHandler.setAttribute("path", this.getPath(this.chosenApp));
  809.         otherHandler.label = this.chosenApp.leafName;
  810.         this.dialogElement("openHandler").selectedIndex = 1;
  811.         this.dialogElement("openHandler").setAttribute("lastSelectedItemID", "otherHandler");
  812.         
  813.         this.dialogElement("mode").selectedItem = this.dialogElement("open");
  814.       }
  815.       else {
  816.         var openHandler = this.dialogElement("openHandler");
  817.         var lastSelectedID = openHandler.getAttribute("lastSelectedItemID");
  818.         if (!lastSelectedID)
  819.           lastSelectedID = "defaultHandler";
  820.         openHandler.selectedItem = this.dialogElement(lastSelectedID);
  821.       }
  822.     },
  823.  
  824.     // Turn this on to get debugging messages.
  825.     debug: false,
  826.  
  827.     // Dump text (if debug is on).
  828.     dump: function( text ) {
  829.         if ( this.debug ) {
  830.             dump( text ); 
  831.         }
  832.     },
  833.  
  834.     // dumpInfo:
  835.     doDebug: function() {
  836.         const nsIProgressDialog = Components.interfaces.nsIProgressDialog;
  837.         // Open new progress dialog.
  838.         var progress = Components.classes[ "@mozilla.org/progressdialog;1" ]
  839.                          .createInstance( nsIProgressDialog );
  840.         // Show it.
  841.         progress.open( this.mDialog );
  842.     },
  843.  
  844.     // dumpObj:
  845.     dumpObj: function( spec ) {
  846.          var val = "<undefined>";
  847.          try {
  848.              val = eval( "this."+spec ).toString();
  849.          } catch( exception ) {
  850.          }
  851.          this.dump( spec + "=" + val + "\n" );
  852.     },
  853.  
  854.     // dumpObjectProperties
  855.     dumpObjectProperties: function( desc, obj ) {
  856.          for( prop in obj ) {
  857.              this.dump( desc + "." + prop + "=" );
  858.              var val = "<undefined>";
  859.              try {
  860.                  val = obj[ prop ];
  861.              } catch ( exception ) {
  862.              }
  863.              this.dump( val + "\n" );
  864.          }
  865.     }
  866. }
  867.  
  868. // This Component's module implementation.  All the code below is used to get this
  869. // component registered and accessible via XPCOM.
  870. var module = {
  871.     firstTime: true,
  872.  
  873.     // registerSelf: Register this component.
  874.     registerSelf: function (compMgr, fileSpec, location, type) {
  875.         if (this.firstTime) {
  876.             this.firstTime = false;
  877.             throw Components.results.NS_ERROR_FACTORY_REGISTER_AGAIN;
  878.         }
  879.         compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  880.  
  881.         compMgr.registerFactoryLocation( this.cid,
  882.                                          "Unknown Content Type Dialog",
  883.                                          this.contractId,
  884.                                          fileSpec,
  885.                                          location,
  886.                                          type );
  887.     },
  888.  
  889.     // getClassObject: Return this component's factory object.
  890.     getClassObject: function (compMgr, cid, iid) {
  891.         if (!cid.equals(this.cid)) {
  892.             throw Components.results.NS_ERROR_NO_INTERFACE;
  893.         }
  894.  
  895.         if (!iid.equals(Components.interfaces.nsIFactory)) {
  896.             throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  897.         }
  898.  
  899.         return this.factory;
  900.     },
  901.  
  902.     /* CID for this class */
  903.     cid: Components.ID("{F68578EB-6EC2-4169-AE19-8C6243F0ABE1}"),
  904.  
  905.     /* Contract ID for this class */
  906.     contractId: "@mozilla.org/helperapplauncherdialog;1",
  907.  
  908.     /* factory object */
  909.     factory: {
  910.         // createInstance: Return a new nsProgressDialog object.
  911.         createInstance: function (outer, iid) {
  912.             if (outer != null)
  913.                 throw Components.results.NS_ERROR_NO_AGGREGATION;
  914.  
  915.             return (new nsUnknownContentTypeDialog()).QueryInterface(iid);
  916.         }
  917.     },
  918.  
  919.     // canUnload: n/a (returns true)
  920.     canUnload: function(compMgr) {
  921.         return true;
  922.     }
  923. };
  924.  
  925. // NSGetModule: Return the nsIModule object.
  926. function NSGetModule(compMgr, fileSpec) {
  927.     return module;
  928. }
  929.